<?php
namespace Dashboard\Controller;

use Think\Controller;
use Think\Session\Driver\Memcache;
//加盟合作
class JoinController extends BaseController{
    //加盟合作--分类列表
    public function type()
    {
        $pid = M("join_type")->order("pid")->select();
        $list = get_attr($pid,0);
        $this->assign("list", $list);
        $this->display();
    }
    //加盟合作--添加列表
    public function type_form()
    {
        $currpage = I('currpage');
        $this->assign("currpage", $currpage);
        $pid = M("join_type")->order("pid")->select();
        $fen = get_attr($pid,0);
        $id = I('id');
        if ($id) {
            $list = sel("join_type", " * ", "id = {$id}");
            $this->assign("list", $list);
        }
        $this->assign("pid", $fen);
        $this->display();
    }
   //加盟合作---数据添加
    public function upload(){
        $id = I('id');
        $name = I("name");
        $pid = I("pid");
        if ($id == 0) {
            $res = ins("join_type", array("name" => $name, "create_at" => time(),"pid"=>$pid));
            if ($res) {
                $logs = logs("添加了美容分类：{$name}");
                $this->success("添加成功", U("type"));
            }
        } else {
            $res = up_one("join_type", array("id" => $id), array("name" => $name, "pid"=>$pid,"up_at" => time()));
            if ($res) {
                $logs = logs("修改了美容分类:{$name}");
                $this->success("修改成功", U("type"));
            }
        }
    }
    //资讯信息
    public function info(){
        $a = (int)($_POST["currpage"]);//跳转页数
        if($a != null){
            $currpage = $a;
        }else{
            $currpage = isset($_GET['currpage'])?$_GET['currpage']:1;
        }
        $totalpage = 0;//总页数
        $pagesize  = 10;//每页显示数
        $arr       = currpage("news","type_two_id > 0","$pagesize","$currpage","id desc");
        $totalpage = $arr['totalpage'];
        $list      = $arr['list'];

        if($list){
            foreach ($list as $k=>$v){
                if($v['type_two_id']){
                    $type_name = sel("join_type","name","id = {$v['type_two_id']}");
                    $list[$k]['type_name'] = $type_name['name'];
                }
                if($v['ke_id']){
                    $type_names = sel("goods_zhu","title","id = {$v['ke_id']}");
                    $list[$k]['ke_name'] = $type_names['title'];
                }
            }
        }
        $this->assign("list",$list)->assign("currpage",$currpage)->assign("totalpage",$totalpage);
        $this->display();
    }
    //资讯信息--添加
    public function forms()
    {
        $id = I('id');
        $pid = M("join_type")->field("id,name")->where("pid != 0")->order("pid")->select();
        $video = sels("goods_zhu","id,title");
        $this->assign("type",$pid)->assign("video",$video);
        if ($id) {
            $list = sel("news", " * ", array("id" => $id));
            $this->assign("list", $list);
        }
        $this->display();
    }
    //资讯信息--数据添加
    public function adds(){
        $currpage = I('currpage');
        $data['type_two_id'] = I('type_two_id');//分类名称
        $data['ke_id'] = I('ke_id');//视频名称
        $data['phone'] = I('phone');
        $data['intro'] = I('intro');
        $data['title'] = I('title');
        $data['content'] = I('content');
        $id = I('id');
        //处理图片
        if ($_FILES['img']['name'] != null) {
            $img = I('img');
            unlink($img);
            $info = $this->imgUpload("info/");
            $savename = $info['img']['savename'];
            $savepath = $info['img']['savepath'];
            $data['img'] = "Data/" . $savepath . $savename;
        }
        if ($id == null) {
            $data['create_at'] = time();
            $result = ins("news", $data);
            if ($result) {
                $content = "管理员：" . $_SESSION['admin']['numbers'] . " 于 " . date("Y-m-d H:i:s", time()) . "添加了资讯信息，名称为：‘" . $data['title'] . "’";
                $log = ins("logs", array("content" => $content, "admin_id" => $_SESSION['admin']['id'], "create_at" => time()));
                $this->success("添加成功", U('info'));
            }
        } else {
            $data['up_at'] = time();
            $result = up_one("news", array("id" => $id), $data);
            if ($result) {
                $content = "管理员：" . $_SESSION['admin']['numbers'] . " 于 " . date("Y-m-d H:i:s", time()) . "修改了资讯信息，修改改后的名称为：‘" . $data['title'] . "’";
                $log = ins("logs", array("content" => $content, "admin_id" => $_SESSION['admin']['id'], "create_at" => time()));
                $this->success("修改成功", U('info'));
            } else {
                $this->success("没有信息变更，修改失败");
            }
        }
    }
}